package program;

import kernel.IOManager;
import kernel.exception.OSException;
import kernel.process.AbstractProcess;
import kernel.process.AbstractProcessHeader;

/**
 * Login process.
 * For more see man() method.
 * @author Jaromir Lepic, Veronika Vencurikova
 *
 */
public class Login extends AbstractProcess {

	public Login(AbstractProcessHeader header, long pid) {
		super(header, pid);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String continueJob() throws OSException {
		shouldEnd = true;
		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected String finalizeJob() throws OSException {
		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected String doJob(String input) throws OSException {
		String user = IOManager.getInstance().requestSTDInputLine("User name: ", shellID, dispatcher, false);
		if (!shouldEnd)
			IOManager.getInstance().requestSTDInputLine("Password: ", shellID, dispatcher, true);
		if (user.trim().equals("")){
			throw new OSException("Invalid user name"); 
		} else {
			IOManager.getInstance().setShellUser(shellID, user);
		}
		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean needParams() {
		return false;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void kill() throws OSException {

	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String man(String command) {
	String man = "";
			
			man += "    signs on\n";
			man += "\n";
			man += "Command syntax:\n";
			man += "     login\n";
		
			
			return man;
	}

}
